define([

        'text!app/tpl/recommedOrgList.html',
        'app/utils/UploadImgUtils',
        "app/service/OrganizationService"
        ], function (
        recommedOrgListTpl,
        UploadImgUtils,
        OrganizationService
    ) {

	var opt = {};

	function beforeuploadFunc(up,file){
		$.processing("图标上传中...", 1);
	}

	function uploadedFunc(info){
		$.hidePreloader();
		// Object {hash: "FtRJBAplyOckAHWWLts855RvdOy7", key: "o_1aonhfltv186g1n9gp7k1se01qf7f.jpg"}
		$("#avatar_img").attr("src",$.wjtBaseCdn + info.key+"?imageView2/5/w/118/h/118/q/100/format/jpg/interlace/1");
		var img = $("#avatar_img").clone();
		$("#avatar_img").remove();
		$("#avatar_container").append(img);
		
		UploadImgUtils.init("avatar_img","haojsqiniu",beforeuploadFunc,uploadedFunc, errorFunc);
	}

	function errorFunc(up, err, errTip){
		$.hidePreloader();
		if(err.code == -600){
			$.toast("上传的图片大小不能超过6MB");
		}else{
			$.toast("您的网络连接不太稳定，请重试");
		}
	}


	return {


		directive_createOrgButton: function(){

			$(".create-org").click(function(){
				var logourl = $("#avatar_img").attr("src");
				var name = $(".name").val();
				var des = $(".des").val();
				var unionid = $.getHashUrlParam("unionid");


				if($.isNullStr(name)){
					$.toast("微讲堂名称不能为空");
					return;
				}
				if($.isNullStr(des)){
					$.toast("微讲堂介绍不能为空");
					return;
				}


				$.processing("创建中...", 1);
				OrganizationService.createOrganization(logourl, name, des, unionid, function(resp){
					
					$.hidePreloader();

					if(resp.status=="500"){
						if(resp.result == "exceed"){
							
							$.promptHelpAssistant("您最多可以创建1个微讲堂，每个微讲堂可以有多个直播话题。如果您确实需要多个微讲堂，请联系我们。");		

						}else{

							$.toast("您的网络连接不太稳定，请重试");
						}
					}else if(resp.status == "200"){

						var uuid = resp.result;
						var url = $.wjtBaseFront + "/org/"+uuid+"/manage?unionid=" + $.getHashUrlParam("unionid");
						$.redirectTo(url);

					}else{

						$.toast("您的网络连接不太稳定，请重试");
					}
					

				});



			});

		},
        
        directive_inputName: function(){
			$(".name_cover").click(function(){

				$.promptInput("微讲堂名称：",$(".name").val(), function(value){
					//OK button
					
					$(".name").val(value);
					

				}, null,"text" , "");
			});
        },

        directive_TextareaDes: function(){
			$(".des_cover").click(function(){

				$.promptTextArea("微讲堂介绍：",$(".des").val(), function(value){
					//OK button

					$(".des").val(value);
			

				}, null,"width: 100%;");
			});

        },

        directive_avatar1: function(){
			UploadImgUtils.init("avatar_img","haojsqiniu",beforeuploadFunc,uploadedFunc, errorFunc);
        }
	};

});
